//package com.springboot.centersystem.controller;
//
//import com.springboot.centersystem.entity.FileObject;
//import org.apache.commons.io.FileUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.core.io.Resource;
//import org.springframework.core.io.UrlResource;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.ResponseEntity;
//import org.springframework.util.FileSystemUtils;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
//
//import java.io.File;
//import java.io.IOException;
//import java.net.MalformedURLException;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.nio.file.StandardCopyOption;
//import java.util.List;
//import java.util.UUID;
//import java.util.stream.Collectors;
///*https://www.bilibili.com/video/BV1e7411P7jG/?spm_id_from=333.788.videocard.4*/
//@RestController
//public class FileUploadController {
//
//    //上传文件路径
//    @Value("${file.base.director}")
//    private String fileBaseDirector;
//    private Path fileBasePath;
//
////    /**
////     * 创建目录
////     */
////    @Autowired
////    private void createDirectories(){
////        try {
////            Files.createDirectories(Paths.get(fileBaseDirector));
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
////        this.fileBasePath = Path.getFileName(fileBaseDirector).toString();
////    }
//
//
//    @PostMapping("files")
//    public String upload(MultipartFile file) throws IOException {
//
//        String name = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
//        File outFile = new File(fileBaseDirector + name);
//        FileUtils.copyToFile(file.getInputStream(), outFile);
//        return name;
//    }
//
//    @GetMapping("/loadAll")
//    public ResponseEntity loadAll() throws IOException {
//        List<FileObject> filesAll = Files.walk(Paths.get(fileBaseDirector),1) //遍历根目录,可以修改1为2，3等,遍历目录下所有的子目录
//                .filter(path -> !path.equals(fileBasePath))//不显示根目录的路径(d:\\test\\)，只显示文件
//                .map(path -> {
//                    // 获取文件url
//                    String url = MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,"loadFile",path.getFileName().toString()).build().toString();
//                    //转换成 FileObject 对象列表
//                    FileObject  fileObject = new FileObject(path.getFileName().toString(),url);
//                    return fileObject;
//                }).collect(Collectors.toList());
//        return ResponseEntity.ok(filesAll);
//    }
//
//
//    @DeleteMapping("delete/{fileName}")
//    public ResponseEntity deleteFile(@PathVariable String fileName){
//        //获取文件的路径
//        Path deletePath = fileBasePath.resolve(fileName);
//        Boolean isDelete = Boolean.FALSE;
//        try {
//            //删除
//            isDelete = FileSystemUtils.deleteRecursively(deletePath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return ResponseEntity.ok(isDelete);
//    }
//
//    @GetMapping("/{fileName}")
//    public ResponseEntity loadFile(@PathVariable String fileName){
//        //获取文件的路径
//        Path path = fileBasePath.resolve(fileName);
//        Resource  resource = null;
//        try {
//            //返回资源文件
//            resource = new UrlResource(path.toUri());
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        }
//        return ResponseEntity.ok()
//                .header(HttpHeaders.CONTENT_DISPOSITION,"attachment;filename=\""+resource.getFilename()+"\"")
//                .body(resource);
//    }
//
////    @PostMapping("/upload")
////    public ResponseEntity upload(@RequestParam("file") MultipartFile file){
////        //获取文件名称，根据自己实际情况，可修改文件名为UUID
////        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
////        Path path = Path.of(fileBaseDirector + fileName);
////        try {
////            //上传
////            Files.copy(file.getInputStream(),path,StandardCopyOption.REPLACE_EXISTING);
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
////        return ResponseEntity.ok("OK");
////    }
//
//@GetMapping("/hello")
//    public String hello(){
//        return "hello";
//}
//}
